package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectSqkp;
import com.ruoyi.gcgl.domain.GcProjectSqkpfp;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSqkpService;
import com.ruoyi.gcgl.service.IGcProjectSqkpfpService;

/**
 * 申请开票发票项Controller
 * 
 * @author ruoyi
 * @date 2023-09-04
 */
@Controller
@RequestMapping("/gcgl/sqkpfp")
public class GcProjectSqkpfpController extends BaseController
{
    private String prefix = "gcgl/sqkpfp";

    @Autowired
    private IGcProjectSqkpfpService gcProjectSqkpfpService;
    @Autowired
    private IGcProjectSqkpService gcProjectSqkpService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:sqkpfp:view")
    @GetMapping()
    public String sqkpfp(){
        return prefix + "/sqkpfp";
    }

    /**
     * 查询申请开票发票项列表
     */
    @RequiresPermissions("gcgl:sqkpfp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSqkpfp gcProjectSqkpfp){
        startPage();
        gcProjectSqkpfp.setDeleteFlag(1);
        List<GcProjectSqkpfp> list = gcProjectSqkpfpService.selectGcProjectSqkpfpList(gcProjectSqkpfp);
        
        for(GcProjectSqkpfp sqkpfp:list){
        	GcProject gcProject = gcProjectService.selectGcProjectById(sqkpfp.getGcProjectid());
        	sqkpfp.setStatus(gcProject.getStatus());
        }
        return getDataTable(list);
    }

    /**
     * 导出申请开票发票项列表
     */
    @RequiresPermissions("gcgl:sqkpfp:export")
    @Log(title = "申请开票发票项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSqkpfp gcProjectSqkpfp){
    	gcProjectSqkpfp.setDeleteFlag(1);
        List<GcProjectSqkpfp> list = gcProjectSqkpfpService.selectGcProjectSqkpfpList(gcProjectSqkpfp);
        ExcelUtil<GcProjectSqkpfp> util = new ExcelUtil<GcProjectSqkpfp>(GcProjectSqkpfp.class);
        return util.exportExcel(list, "申请开票发票项数据");
    }

    /**
     * 新增申请开票发票项
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap){
    	
    	GcProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSqkp.getGcProjectid());
    	
    	mmap.put("gcProjectid", gcProject.getId());
    	mmap.put("projectNo", gcProject.getOrderNo());
    	mmap.put("xmmc", gcProject.getName());
    	mmap.put("supplyName1", gcProject.getCustomerName());
    	mmap.put("gcProjectSqkp", gcProjectSqkp);
    	
        return prefix + "/add";
    }

    /**
     * 新增保存申请开票发票项
     */
    @RequiresPermissions("gcgl:sqkpfp:add")
    @Log(title = "申请开票发票项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSqkpfp gcProjectSqkpfp){
    	try {
    		gcProjectSqkpfp.setDeleteFlag(1);
    		gcProjectSqkpfp.setCreateUser(getLoginName());
    		gcProjectSqkpfp.setCreateAt(new Date());
    		gcProjectSqkpfp.setUpdateUser(getLoginName());
    		gcProjectSqkpfp.setUpdateAt(new Date());
    		gcProjectSqkpfpService.insertGcProjectSqkpfp(gcProjectSqkpfp);
        	
		} catch (Exception e) {
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改申请开票发票项
     */
    @RequiresPermissions("gcgl:sqkpfp:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSqkpfp gcProjectSqkpfp = gcProjectSqkpfpService.selectGcProjectSqkpfpById(id);
        if(gcProjectSqkpfp.getFap()!=null && !gcProjectSqkpfp.getFap().equals("")){
        	String[] zlis = gcProjectSqkpfp.getFap().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("fapvo", file);
        }else{
        	mmap.put("fapvo", "");
        }
        mmap.put("gcFkfp", gcProjectSqkpfp);
        
        GcCustomer gcCustomer = new GcCustomer();
        gcCustomer.setDeleteFlag(1);
        gcCustomer.setType(1);
        List<GcCustomer> customerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        mmap.put("customerlis", customerlis);
        
        return prefix + "/edit";
    }
    
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSqkpfp gcProjectSqkpfp = gcProjectSqkpfpService.selectGcProjectSqkpfpById(id);
        if(gcProjectSqkpfp.getFap()!=null && !gcProjectSqkpfp.getFap().equals("")){
        	String[] zlis = gcProjectSqkpfp.getFap().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("fapvo", file);
        }else{
        	mmap.put("fapvo", "");
        }
        mmap.put("gcFkfp", gcProjectSqkpfp);
        
        return prefix + "/detail";
    }

    /**
     * 修改保存申请开票发票项
     */
    @RequiresPermissions("gcgl:sqkpfp:edit")
    @Log(title = "申请开票发票项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSqkpfp gcProjectSqkpfp){
    	try {
    		gcProjectSqkpfp.setUpdateUser(getLoginName());
    		gcProjectSqkpfp.setUpdateAt(new Date());
    		gcProjectSqkpfpService.updateGcProjectSqkpfp(gcProjectSqkpfp);
		} catch (Exception e) {
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除申请开票发票项
     */
    @RequiresPermissions("gcgl:sqkpfp:remove")
    @Log(title = "申请开票发票项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSqkpfpService.deleteGcProjectSqkpfpByIds(ids));
    }
    
    @GetMapping("/p4sqkpfp/{id}")
    public String p4sqkpfp(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return "sj/project/projectDetailSqkpfp";
    }

    /**
     * 查询申请开票发票项列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(GcProjectSqkpfp gcProjectSqkpfp){
        startPage();
        gcProjectSqkpfp.setDeleteFlag(1);
        List<GcProjectSqkpfp> list = gcProjectSqkpfpService.selectGcProjectSqkpfpList(gcProjectSqkpfp);
        
        for(GcProjectSqkpfp sqkpfp:list){
        	GcProject gcProject = gcProjectService.selectGcProjectById(sqkpfp.getGcProjectid());
        	sqkpfp.setStatus(gcProject.getStatus());
        }
        return getDataTable(list);
    }
}